from conans import ConanFile, CMake, tools, AutoToolsBuildEnvironment
from conans import tools
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path
import shutil


class LibeventConan(ConanFile):
    name = "lua"
    version = "1.1.1"
    url = "https://gitee.com/290198252/lua.git"
    description = "lua"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"
    
    def source(self):
        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            tools.unzip("lua-5.4.2.tar.gz")
    def build(self):
        cmake = CMake(self)
        print("path " + self.source_folder + " is where the package built")
        print("build env is ", tools.get_env("BUILD_ENV"))
        tools.rmdir(tools.unix_path(self.package_folder)+"/build")
        tools.mkdir(tools.unix_path(self.package_folder)+"/build")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                if tools.get_env("CONAN_SH_PATH") == "":
                    print("msys openssl build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="lua-5.4.2", msys_mingw=True,
                                              bashcmd="make mingw  ")
                os.remove(tools.get_env("CONAN_SH_PATH"))
            if tools.get_env("BUILD_ENV") == "msvc64":
                print("msvc64 build")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="lua-5.4.2", msys_mingw=True,
                                              bashcmd="make mingw  ")
                os.remove(tools.get_env("CONAN_SH_PATH"))
    def package(self):
        self.copy("*.a","lib",keep_path=False)
        self.copy("*.h","include",keep_path=False)
        self.copy("*.exe","bin",keep_path=False)
        self.copy("*.dll","bin",keep_path=False)
        self.copy("*.so","bin",keep_path=False)

    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe",dst="",keep_path=True)
        self.copy("lua-5.4.2.tar.gz",dst="",keep_path=True)

    def package_info(self):
        self.cpp_info.name = 'lua'
        self.cpp_info.libs = ['lua.a']  # The libs to link against

        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "lua"
                self.cpp_info.names["cmake_find_package_multi"] = "lua"
                self.cpp_info.libdirs = ['pkgsrc']  # Directories where libraries can be found
                self.cpp_info.libs = ['lua.a']  # The libs to link against
                self.cpp_info.buildirs = 'pkgsrc'
